'use strict';

const ms = require('ms');

module.exports = app => {
  return class Model {
    constructor(ctx) {
      this.ctx = ctx;
    }
    async getClient(clientId, clientSecret) {
      console.log('getClient');
      const { grants } = app.config.oauth2Server;
      const client = { clientId, clientSecret, grants };
      if (clientId !== 'randy' || clientSecret !== '0312') {
        return;
      }
      return client;
    }
    async getUser(username, password) {
      console.log('getUser');
      if (username !== 'admin' || password !== '123123') {
        return;
      }
      return { id: '1' };
    }
    async getAccessToken(bearerToken) {
      console.log(`getAccessToken: ${bearerToken}`);
      const _token = this.ctx.session.OAuthToken;
      _token.accessTokenExpiresAt = new Date(_token.accessTokenExpiresAt);
      _token.refreshTokenExpiresAt = new Date(_token.refreshTokenExpiresAt);
      return _token;
    }
    async saveToken(token, client, user) {
      console.log('saveToken');
      const _token = Object.assign({}, token, { user }, { client });
      this.ctx.session.OAuthToken = _token;
      return _token;
    }
  };
};
